﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class ManufacturerGroupBiz
    {
        #region Properties

        private readonly IManufacturerGroupDao _manufacturerGroupDao;

        #endregion

        #region Constructors

        public ManufacturerGroupBiz()
        {
            _manufacturerGroupDao = new ManufacturerGroupDao();
        }

        public ManufacturerGroupBiz(IManufacturerGroupDao manufacturerGroupDao)
        {
            _manufacturerGroupDao = manufacturerGroupDao;
        }

        #endregion

        #region Methods

        public ManufacturerGroup GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _manufacturerGroupDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public ManufacturerGroup CreateGroup(ManufacturerGroup manufacturerGroup)
        {
            // validate inputs
            if (manufacturerGroup == null) return null;
            if (string.IsNullOrEmpty(manufacturerGroup.Code) || string.IsNullOrEmpty(manufacturerGroup.Name)) return null;

            try
            {
                return _manufacturerGroupDao.AddManufacturerGroup(manufacturerGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _manufacturerGroupDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _manufacturerGroupDao.GetCurrentIdentity() + 1;
        }

        public void UpdateGroup(ManufacturerGroup manufacturerGroup)
        {
            if (manufacturerGroup == null) return;
            try
            {
                _manufacturerGroupDao.UpdateManufacturerGroup(manufacturerGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetAllManufacturerGroupsWs()
        {
            try
            {
                var peopleList = _manufacturerGroupDao.GetAllManufacturerGroups();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateGroupWs(ManufacturerGroup manufacturerGroup)
        {
            // validate inputs
            if (manufacturerGroup == null) return null;
            if (string.IsNullOrEmpty(manufacturerGroup.Code) || string.IsNullOrEmpty(manufacturerGroup.Name)) return null;

            try
            {
                var addedManufacturerGroup = _manufacturerGroupDao.AddManufacturerGroup(manufacturerGroup);
                return new { Result = "OK", Record = addedManufacturerGroup };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateGroupWs(ManufacturerGroup manufacturerGroup)
        {
            if (manufacturerGroup == null) return null;
            try
            {
                _manufacturerGroupDao.UpdateManufacturerGroup(manufacturerGroup);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteGroupWs(int manufacturerGroupId)
        {
            try
            {
                _manufacturerGroupDao.DeleteManufacturerGroup(manufacturerGroupId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers

        #endregion
    }
}
